package com.dalonedrow.engine.ui.impl.border;

import com.dalonedrow.engine.sprite.base.SimpleVector3;
import com.dalonedrow.engine.sprite.base.SpriteImageObject;
import com.dalonedrow.engine.sprite.base.SpriteImageObjectFactory;
import com.dalonedrow.engine.sprite.impl.BasicQuadSpriteObject;
import com.dalonedrow.engine.sprite.impl.BasicSpriteObject;
import com.dalonedrow.engine.sprite.impl.GroupOfImages;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.Render;
import com.dalonedrow.engine.ui.base.border.GuiBorder;
import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;

/**
 * A simple border, similar to those found in the early 90's SNES games.
 * @author Donald
 */
public final class SNESBorder extends GuiBorder {
	/** the border group. */
	private GroupOfImages			borders;
	/** the middle sprite. */
	private BasicQuadSpriteObject	middle;
	/** the x-offset for the middle graphic. */
	private int						middleInsetX;
	/** the y-offset for the middle graphic. */
	private int						middleInsetY;
	/** Creates a new instance of {@link SNESBorder}. */
	public SNESBorder() {
		final int eight = 8;
		super.setInsets(
				eight,
				eight,
				eight,
				eight
				);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(borders);
		Render.getInstance().registerEntity(middle);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y, final double z) {
		super.setPosition(x, y, z);
		borders.setPosition(x, y, z);
		borders.clearFloatStore();
		middle.setPosition(x + middleInsetX, y + middleInsetY, z);
		middle.clearFloatStore();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int width, final int height) {
		super.setPreferredSize(width, height);
		try {
			final int three = 3, four = 4, five = 5, six = 6, seven = 7,
					eight = 8;
			SpriteImageObject imgBL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_bl");
			SpriteImageObject imgBR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_br");
			SpriteImageObject imgB =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_b");
			SpriteImageObject imgTL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_tl");
			SpriteImageObject imgTR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_tr");
			SpriteImageObject imgT =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_t");
			SpriteImageObject imgL =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_l");
			SpriteImageObject imgR =
					SpriteImageObjectFactory.getInstance().getImageByName(
							"nes_border_r");
			if (borders == null) {
				borders = new GroupOfImages();
				// sprite images don't get set until the preferred size is set
				middleInsetX = imgL.getWidth();
				middleInsetY = imgB.getHeight();

				{ // bottom left
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgBL.getRefId());
					sb.setWidth(imgBL.getWidth());
					sb.setHeight(imgBL.getHeight());
					borders.addImage(sb);
				}
				{ // bottom middle
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgB.getRefId());
					sb.setHeight(imgB.getHeight());
					borders.addImage(sb);
				}
				{ // bottom right
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgBR.getRefId());
					sb.setWidth(imgBR.getWidth());
					sb.setHeight(imgBR.getHeight());
					borders.addImage(sb);
				}
				{ // left
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgL.getRefId());
					sb.setWidth(imgL.getWidth());
					borders.addImage(sb);
				}
				{ // right
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgR.getRefId());
					sb.setWidth(imgR.getWidth());
					borders.addImage(sb);
				}
				{ // top left
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgTL.getRefId());
					sb.setWidth(imgTL.getWidth());
					sb.setHeight(imgTL.getHeight());
					borders.addImage(sb);
				}
				{ // top middle
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgT.getRefId());
					sb.setHeight(imgT.getHeight());
					borders.addImage(sb);
				}
				{ // top right
					BasicSpriteObject sb = new BasicSpriteObject();
					sb.setImageRefId(imgTR.getRefId());
					sb.setWidth(imgTR.getWidth());
					sb.setHeight(imgTR.getHeight());
					borders.addImage(sb);
				}
				{ // middle
					middle = new BasicQuadSpriteObject();
					middle.setRed(GlobalConstants.COL_SNES_BORDER[0]);
					middle.setGreen(GlobalConstants.COL_SNES_BORDER[1]);
					middle.setBlue(GlobalConstants.COL_SNES_BORDER[2]);
					middle.setAlpha(GlobalConstants.COL_SNES_BORDER[three]);
				}
			}
			// set bottom-left position
			borders.getImage(0).setPosition(new SimpleVector3(0, 0, 0));
			// set bottom position and width
			borders.getImage(1).setWidth(
					width - imgBL.getWidth() - imgBR.getWidth());
			borders.getImage(1).setPosition(
					new SimpleVector3(imgBL.getWidth(), 0, 0));
			// set bottom-right position
			borders.getImage(2).setPosition(new SimpleVector3(
					width - imgBR.getWidth(), 0, 0));
			// set left position and height
			borders.getImage(three).setHeight(
					height - imgBL.getHeight() - imgTL.getHeight());
			borders.getImage(three).setPosition(
					new SimpleVector3(0, imgBL.getHeight(), 0));
			// set right position and height
			borders.getImage(four).setHeight(
					height - imgBR.getHeight() - imgTR.getHeight());
			borders.getImage(four).setPosition(
					new SimpleVector3(width - imgR.getWidth(), imgBR.getHeight(), 0));
			// set top-left position
			borders.getImage(five).setPosition(
					new SimpleVector3(0, height - imgTL.getHeight(), 0));
			// set top position and width
			borders.getImage(six).setWidth(
					width - imgTL.getWidth() - imgTR.getWidth());
			borders.getImage(six)
					.setPosition(
							new SimpleVector3(imgTL.getWidth(), height
									- imgT.getHeight(), 0));
			// set top-right position
			borders.getImage(seven).setPosition(
					new SimpleVector3(width - imgTR.getWidth(),
							height - imgTR.getHeight(), 0));
			// set middle position and width
			middle.setWidth(width - imgL.getWidth() - imgR.getWidth());
			middle.setHeight(height - imgB.getHeight() - imgT.getHeight());
			if (super.getPosition() != null) {
				borders.setPosition(
						super.getPosition().getX(),
						super.getPosition().getY(),
						super.getPosition().getZ()
						);
				borders.clearFloatStore();
				middle.setPosition(
						super.getPosition().getX() + middleInsetX,
						super.getPosition().getY() + middleInsetY,
						super.getPosition().getZ()
						);
				middle.clearFloatStore();
			}
			imgTL = null;
			imgT = null;
			imgTR = null;
			imgL = null;
			imgR = null;
			imgBL = null;
			imgB = null;
			imgBR = null;
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
